﻿Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Imports Commander_Wars.Spieletools
Public Class ObjektNumericUpDown
    Inherits Objekt2D
    Const ReglerBreite As Integer = 16
    Const Schnellerzähler As Single = 0.5
    Const MaxGeschwindigkeit As Integer = 10
    Const FramenMax As Integer = 12
    Dim Value As Integer
    Public ReadOnly Property GetValue() As Integer
        Get
            If Value <= Infinite Then
                Return -1
            Else
                Return Value
            End If
        End Get
    End Property
    Public Sub SetValue(ByVal Wert As Integer)
        Value = Wert
        If Value > Max Then Value = Max
        If Value < Min Then Value = Min
        If Value <= Infinite Then
            Text.neuerText(New String() {"∞"}, 2, 1, Nothing)
        Else
            Text.neuerText(New String() {Value}, 2, 1, Nothing)
        End If
    End Sub
    Dim Aktiv As Boolean = False
    Dim Zählgeschwindigkeit As Double = 0.5
    Dim Min As Integer
    Dim Max As Integer
    Dim Text As ObjektText
    Dim Änderung As Integer
    Dim Ausgewäht As Boolean
    Dim Infinite As Integer = -1
    Private Framens As Integer
    Public Property Framen() As Integer
        Get
            Return Framens
        End Get
        Set(ByVal value As Integer)
            Framens += 1
            If Framens > FramenMax Then Framens = 0
        End Set
    End Property

    Public Sub New(ByVal Startvalue As Integer, ByVal Startmin As Integer, ByVal Startmax As Integer, ByVal Bild As String, ByVal Position As Point, ByVal neueÄnderung As Integer, ByVal Fixed As PointF, Optional ByVal Infinite As Integer = -1, Optional ByVal Handler As ObjektPanel = Nothing)
        Me.New(Startvalue, Startmin, Startmax, Image.FromFile(Bild), Position, neueÄnderung, Bild, Fixed, Infinite, Handler)
    End Sub
    Public Sub New(ByVal Startvalue As Integer, ByVal Startmin As Integer, ByVal Startmax As Integer, ByVal Bild As Bitmap, ByVal Position As Point, ByVal neueÄnderung As Integer, ByVal Bildname As String, ByVal Fixed As PointF, Optional ByVal Infinite As Integer = -1, Optional ByVal Handler As ObjektPanel = Nothing)
        MyBase.New(Bildname, New Rectangle(0, 0, Bild.Width, Bild.Height), Position, Fixed, Nothing)
        Value = Startvalue
        Min = Startmin
        Max = Startmax
        Me.Infinite = Infinite
        Änderung = neueÄnderung
        If Startvalue < Min Then
            Value = Min
        End If
        If Value <= Infinite Then
            Text = New ObjektText(New String() {"∞"}, 2, 1, Nothing, "Arial", 14, FontStyle.Regular)
        Else
            Text = New ObjektText(New String() {Value.ToString}, 2, 1, Nothing, "Arial", 14, FontStyle.Regular)
        End If
        Bildgröße = New Rectangle(0, 0, Text.GetSchriftart.MeasureString(Schriftengine.Sprite2, Max.ToString, DrawTextFormat.None, Color.Black).Width + 4, 20)
        'Da Größen und andere Dinge erst noch berechnet wurden!
        Me.Handler = Handler
        If Me.Handler IsNot Nothing Then
            Me.Handler.AddObjekt(Me)
        End If
    End Sub
    Public Overrides Sub Malen()
        If Handler IsNot Nothing Then
            If Not Handler.IsVisible(New Rectangle(Koordinaten.X, Koordinaten.Y, Rechteck.Width + 16, Rechteck.Height), True) Then
                Exit Sub
            End If
        End If
        Dim sngRotation As Double = 0
        Dim intColor As Color = Color.FromArgb(255, 255, 255, 255)
        Grafikengine.ptfRotCenter = Nothing
        Grafikengine.szfScale = Nothing
        Grafikengine.ptfPosition = GetRealKoordinaten()
        With Grafikengine.recBild
            .X = 0 : .Width = 2
            .Y = 0 : .Height = 20
        End With
        Grafikengine.Sprite.Draw2D(Textur.getTextur(TexturID), Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
        Text.Malen(Grafikengine.Sprite)
        Grafikengine.ptfPosition.X += Rechteck.Width + 15
        Grafikengine.recBild.X = 30
        Grafikengine.Sprite.Draw2D(Textur.getTextur(TexturID), Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
        Grafikengine.ptfPosition.X = GetRealX() + 2
        With Grafikengine.recBild
            .X = 2 : .Width = 13
            .Y = 0 : .Height = 20
        End With
        Grafikengine.szfScale.Width = Rechteck.Width
        Grafikengine.szfScale.Height = 20
        Grafikengine.Sprite.Draw2D(Textur.getTextur(TexturID), Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
        With Grafikengine.recBild
            .X = 16 : .Width = 14
            .Y = 0 : .Height = 20
        End With
        Grafikengine.szfScale = Nothing
        Grafikengine.ptfPosition.X = GetRealX() + 1 + Rechteck.Width
        Grafikengine.Sprite.Draw2D(Textur.getTextur(TexturID), Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
        'wir lassen den Zähler immer schneller werden.=)
        If Max >= 0 Then
            If Aktiv = True Then
                If Framen = 0 Then
                    If Zählgeschwindigkeit > 0 Then
                        Zählgeschwindigkeit += Schnellerzähler
                        If Zählgeschwindigkeit > MaxGeschwindigkeit Then Zählgeschwindigkeit = MaxGeschwindigkeit
                    Else
                        Zählgeschwindigkeit -= Schnellerzähler
                        If Zählgeschwindigkeit < -MaxGeschwindigkeit Then Zählgeschwindigkeit = -MaxGeschwindigkeit
                    End If
                    Dim Änderung2 As Integer = Round(Zählgeschwindigkeit * Änderung, Änderung)
                    If Änderung2 = 0 Then
                        If Zählgeschwindigkeit > 0 Then
                            Änderung2 = Änderung
                        Else
                            Änderung2 = -Änderung
                        End If
                    End If
                    Value += Änderung2
                    If Value > Max Then Value = Max
                    If Value < Min Then Value = Min
                    If Value <= Infinite Then
                        Text.neuerText(New String() {"∞"}, 2, 1, Nothing)
                    Else
                        Text.neuerText(New String() {Value}, 2, 1, Nothing)
                    End If
                End If
                Framen = 1
            End If
        End If
    End Sub
    Public Overrides Function Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean

    End Function

    Public Overrides Function KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean

    End Function

    Public Overloads Overrides Function MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) As Boolean
        If IsVisble(16) Then
            If (Not Objekt2D.AlreadyKlickt And Not HasFocus) Or Focused Then
                Dim P As Point = GetRealKoordinaten()
                Dim StartRechteck As New Rectangle(P.X + Rechteck.Width, P.Y, ReglerBreite, 6)
                If Rechteck.Contains(e.X, e.Y) Then
                    Objekt2D.AlreadyKlickt = True
                Else
                    If StartRechteck.Contains(e.X, e.Y) Then
                        Aktiv = True
                        If Value <= Infinite Then
                            Text.neuerText(New String() {"∞"}, 2, 1, Nothing)
                        Else
                            Text.neuerText(New String() {Value}, 2, 1, Nothing)
                        End If
                        Objekt2D.AlreadyKlickt = True
                        Return True
                    Else
                        StartRechteck.Y += 10
                        If StartRechteck.Contains(e.X, e.Y) Then
                            Aktiv = True
                            Zählgeschwindigkeit = -0.5#
                            If Value <= Infinite Then
                                Text.neuerText(New String() {"∞"}, 2, 1, Nothing)
                            Else
                                Text.neuerText(New String() {Value}, 2, 1, Nothing)
                            End If
                            Objekt2D.AlreadyKlickt = True
                            Return True
                        End If
                    End If
                End If
                Return False
            End If
        End If
        Return False
    End Function


    Public Overloads Overrides Function MouseEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) As Boolean

    End Function

    Public Overloads Overrides Function MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) As String
        Aktiv = False
        Zählgeschwindigkeit = 0.5
        Framens = 0
        Return ""
    End Function

  
End Class
